package com.example.controller;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import com.example.common.Result;
import com.example.model.bo.Record;

import com.example.service.DataOwnService;
import com.example.service.impl.DataOwnServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
public class DataOwnController {
    @Autowired
    DataOwnService dataOwnService;

    /**
     * 查询溯源上传的历史记录
     * @return
     * @throws Exception
     */
    @PostMapping("/data")
    public Result getOwnData() throws Exception {
        //调用Service查询历史的溯源上传记录
        List<Record> ownData = dataOwnService.getOwnData();

        // 将 hashKey 转换为字符串
        ownData.forEach(record -> {
            List<String> hashKeyStr = record.getHashKey().stream()
                    .map(BigInteger::toString)
                    .collect(Collectors.toList());
            record.setHashKeyStr(new ArrayList<>(hashKeyStr)); // 设置 hashKeyStr
        });

        return Result.success(ownData);
    }

}
